package top.yangbuyi.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import top.yangbuyi.acpect.annotaction.Log;
import top.yangbuyi.constants.Constants;
import top.yangbuyi.dto.UserDto;
import top.yangbuyi.mapper.RoleMapper;
import top.yangbuyi.mapper.UserMapper;
import top.yangbuyi.domain.User;
import top.yangbuyi.service.impl.UserService;
import top.yangbuyi.utils.AppMd5Utils;
import top.yangbuyi.vo.DataGridView;

/**
 * ClassName: Loginfo
 * Description: 杨不易网站 :www.yangbuyi.top
 * date: 2020/8/27
 *
 * @author TeouBle
 * @author yangbuyi
 * @since JDK 1.8
 **/

@Service
public class UserServiceImpl implements UserService {

	  @Autowired
	  private UserMapper userMapper;

	  @Autowired
	  private RoleMapper roleMapper;

	  /**
	   * 根据手机号查询用户
	   *
	   * @param phone 手机号
	   * @return
	   */
	  @Override
	  public User queryUserByPhone(String phone) {
			QueryWrapper<User> qs = new QueryWrapper<>();
			qs.eq(User.COL_PHONE, phone);
			return this.userMapper.selectOne(qs);
	  }

	  /**
	   * 根据用户ID查询用户
	   *
	   * @param userId 用户编号
	   * @return
	   */
	  @Override
	  public User getOne(Long userId) {
			return this.userMapper.selectById(userId);
	  }

	  /**
	   * 分页查询用户
	   *
	   * @param userDto
	   * @return
	   */
	  @Override
	  public DataGridView listUserForPage(UserDto userDto) {
			Page<User> page = new Page<>(userDto.getPageNum(), userDto.getPageSize());
			QueryWrapper<User> qw = new QueryWrapper<>();
			qw.like(StringUtils.isNotBlank(userDto.getUserName()), User.COL_USER_NAME, userDto.getUserName());
			qw.like(StringUtils.isNotBlank(userDto.getPhone()), User.COL_PHONE, userDto.getPhone());
			qw.eq(StringUtils.isNotBlank(userDto.getStatus()), User.COL_STATUS, userDto.getStatus());
			qw.eq(userDto.getDeptId() != null, User.COL_DEPT_ID, userDto.getDeptId());
			/* 查询时间段 time>xxx and xxx<time */
			qw.ge(null != userDto.getBeginTime(), User.COL_CREATE_TIME, userDto.getBeginTime());
			qw.le(null != userDto.getEndTime(), User.COL_CREATE_TIME, userDto.getEndTime());
			qw.orderByAsc(User.COL_USER_ID);
			this.userMapper.selectPage(page, qw);
			return new DataGridView(page.getTotal(), page.getRecords());

	  }

	  /**
	   * 添加用户
	   *
	   * @param userDto
	   * @return
	   */
	  @Override
	  public int addUser(UserDto userDto) {
			User user = new User();
			BeanUtil.copyProperties(userDto, user);
			user.setUserType(Constants.USER_NORMAL);
			user.setCreateBy(userDto.getSimpleUser().getUserName());
			user.setCreateTime(DateUtil.date());
			user.setSalt(AppMd5Utils.createSalt());
			String defaultPwd = user.getPhone().substring(5);
			user.setPassword(AppMd5Utils.md5(defaultPwd, user.getSalt(), 2));
			return this.userMapper.insert(user);
	  }

	  /**
	   * 修改用户
	   *
	   * @param userDto
	   * @return
	   * @保证数据唯一
	   */
	  @Override
	  public int updateUser(UserDto userDto) {
			// 查询出唯一数据
			User user = this.userMapper.selectById(userDto.getUserId());
			if (null == user) {
				  return 0;
			}
			// 浅克隆
			BeanUtil.copyProperties(userDto, user);
			user.setUpdateBy(userDto.getSimpleUser().getUserName());
			return this.userMapper.updateById(user);
	  }

	  /**
	   * 删除用户
	   *
	   * @param userIds
	   * @return
	   */
	  @Override
	  public int deleteUserByIds(Long[] userIds) {
			List<Long> ids = Arrays.asList(userIds);
			// 删除用户需要删除与之关联的表数据sys_role_user 角色
			this.roleMapper.deleteRoleUserByUserIds(ids);
			return this.userMapper.deleteBatchIds(ids);
	  }

	  /**
	   * 查询所有可用的用户
	   *
	   * @return
	   */
	  @Override
	  public List<User> getAllUsers() {
			QueryWrapper<User> qw = new QueryWrapper<>();
			qw.eq(User.COL_STATUS, Constants.STATUS_TRUE);
			qw.eq(User.COL_USER_TYPE, Constants.USER_NORMAL);
			qw.orderByAsc(User.COL_USER_ID);
			return this.userMapper.selectList(qw);
	  }

	  /**
	   * 重置用户密码
	   *
	   * @param userIds
	   */
	  @Override
	  public void resetPassWord(Long[] userIds) {
			for (Long userId : userIds) {
				  User user = this.userMapper.selectById(userId);
				  String defaultPwd = "";
				  if (user.getUserType().equals(Constants.USER_ADMIN)) {
						defaultPwd = "yjy123";
				  } else {
						defaultPwd = user.getPhone().substring(5);
				  }
				  user.setSalt(AppMd5Utils.createSalt());
				  user.setPassword(AppMd5Utils.md5(defaultPwd, user.getSalt(), 2));
				  this.userMapper.updateById(user);
			}
	  }

	@Override
	public List<User> queryUsersNeedScheduling(Long userId, Long deptId) {
		QueryWrapper<User> wrapper=new QueryWrapper<>();
		wrapper.eq(null!=deptId, User.COL_DEPT_ID,deptId);
		wrapper.eq(null!=userId, User.COL_USER_ID,userId);
		wrapper.eq(User.COL_SCHEDULING_FLAG, Constants.SCHEDULING_FLAG_TRUE);//需要排班的
		wrapper.eq(User.COL_STATUS, Constants.STATUS_TRUE);//可用的用户
		return this.userMapper.selectList(wrapper);

	}
}